﻿/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
CKEDITOR.dialog.add('dialogwindow', function (editor) {
    // Add a new option to a SELECT object (combo or list).
    var _fieldName = '';
    var _caption = '';
    var _idName = '';
    var _datasourcetype = '';
    var _datasource = '';
    var typevalues = '';
    var autoAttributes =
	{
	    value: 1,
	    size: 1,
	    maxLength: 1
	};
    var acceptedTypes =
	{
	    text: 1,
	    password: 1,
	    combox: 1,
        dialogwindow:1
	};
    function getSelect(obj) {
        if (obj && obj.domId && obj.getInputElement().$)				// Dialog element.
            return obj.getInputElement();
        else if (obj && obj.$)
            return obj;
        return false;
    }

    return {
        title: editor.lang.dialogwindow.title,
        minWidth: CKEDITOR.env.ie ? 460 : 395,
        minHeight: CKEDITOR.env.ie ? 320 : 300,
        onShow: function () {
            delete this.combox;

            var element = this.getParentEditor().getSelection().getSelectedElement();
            if (element && element.getName() == "input" &&
					(acceptedTypes[element.getAttribute('type')] || !element.getAttribute('type'))) {
                this.combox = element;
                this.setupContent(element);
            }

        },
        onOk: function (element) {
            var editor,
				element = this.comboxid,
				isInsertMode = !element;

            if (isInsertMode) {
                editor = this.getParentEditor();
                element = editor.document.createElement('input');
                element.setAttribute('type', 'dialogwindow');
                element.setAttribute('typevalue', typevalues);
            }

            if (isInsertMode)
                editor.insertElement(element);
            this.commitContent({ element: element });
        },
        onLoad: function () {
            var autoSetup = function (element) {
                var value = element.hasAttribute(this.id) && element.getAttribute(this.id);
                this.setValue(value || '');
            };
            var autoCommit = function (data) {
                var element = data.element;
                var value = this.getValue();

                if (value)
                    element.setAttribute(this.id, value);
                else
                    element.removeAttribute(this.id);
            };
        },
        contents: [
			{
			    id: 'info',
			    label: editor.lang.dialogwindow,
			    title: editor.lang.dialogwindow,
			    accessKey: '',
			    elements: [
                	 {
                	     id: 'datafields',
                	     type: 'select',
                	     style: 'width:100%;',
                	     label: editor.lang.checkboxAndRadio.datafields,
                	     'default': 'text',
                	     accessKey: 'D',
                	     items: DIF.MasterData.getMasterFieldData(),
                	     onChange: function (obj) {
                	         _fieldName = this.getValue();
                	         var isExist = $.inArray(this.getValue(), ControlArray);
                	         if (isExist < 0) {
                	             var dialog = this.getDialog();
                	             var selectValue = this.getValue();

                	             var Item = jlinq.from(this.items).equals('1', selectValue).select();
                	             if (typeof (Item) != "undefined") {
                	                 if (typeof (Item[0]) != "undefined") { _caption = Item[0][0]; }
                	             }
                	             var textfieldName = dialog.getContentElement('info', 'txtName');
                	             textfieldName.setValue(selectValue);
                	             textfieldName.getElement().show();
                	         } else {
                	             if (obj.data.value != _idName) {
                	                 alert(editor.lang.select.isexist_datafields);
                	             }
                	         }
                	     },
                	     setup: function (element) {
                	         _idName = element.getAttribute('id');
                	         this.setValue(element.getAttribute('datafields'));
                	     },
                	     commit: function (data) {
                	         var element = data.element;
                	         if (this.getValue()) {
                	             element.setAttribute('datafields', this.getValue());
                	             element.setAttribute('title', _caption);
                	         }
                	         else {
                	             element.removeAttribute('datafields');
                	             element.removeAttribute('title');
                	         }
                	     }
                	 },
					{
					    id: 'txtName',
					    type: 'text',
					    label: editor.lang.common.name,
					    'default': '',
					    accessKey: 'N',
					    setup: function (element) {
					        this.setValue(
									element.data('cke-saved-name') ||
									element.getAttribute('name') ||
									'');
					    },
					    commit: function (data) {
					        var element = data.element;

					        // IE failed to update 'name' property on input elements, protect it now.
					        if (this.getValue()) {
					            element.data('cke-saved-name', this.getValue());
					            element.setAttribute('id', this.getValue());
					        }
					        else {
					            element.data('cke-saved-name', false);
					            element.removeAttribute('name');
					            element.removeAttribute('id');
					        }
					    }
					},
					{
					    id: 'dataSourcetype',
					    type: 'select',
					    widths: ['21%', '75%'],
					    style: 'width:86%;',
					    labelLayout: 'horizontal',
					    label: editor.lang.select.dataSourcetype,
					    'default': '',
					    accessKey: 'D',
					    items: [
                                ['客户', 'o'],
                                ['公司', 'm'],
							    ['部门', 'p'],
                                ['岗位', 's']
						    ],
					    onChange: function (obj) {
					        typevalues = this.getValue();
					    },
					    setup: function (name, element) {

					        if (_fieldName != '') {
					            values = this.setValue();
					            var item = DIF.getFieldItem(_fieldName);
					            if (item) {
					                if (!item.DataSourceType) { this.setValue(); }
					                else { this.setValue(item.DataSourceType); }
					            }
					        }

					    },
					    commit: function (element) {
					        if (this.getValue()) {
					            _datasourcetype = this.getValue();
					            if (_fieldName != '') {
					                var item = DIF.getFieldItem(_fieldName);
					                if (item) {
					                    item.DataSourceType = _datasourcetype;
					                } else {
					                    var _elementType = element.element.getAttribute('type');
					                    DIF.AddFieldItem(
                                        NewGuid(),
                                        DIF.getFormID(),
                                        DIF.getMasterTableName(),
                                        _caption,
                                        _fieldName,
                                        '',
                                        _datasourcetype,
                                        _datasource,
                                        0,
                                        _elementType
                                        );
					                }
					            }

					        }
					        else {
					            if (_fieldName != '') {
					                var item = DIF.getFieldItem(_fieldName);
					                if (item) {
					                    item.DataSourceType = '';
					                }
					            }
					        }
					    }
					}

				]
			}
		]
    };
});
